﻿using System;
using System.IO;
using System.Net;
using Accona.Monitoring.AppStatistics;
using Accona.Monitoring.Exposure.REST;
using Accona.Monitoring.Publishers;
using FluentTests.Framework.VerbExtensions;
using FluentTests.NUnit;
using NUnit.Framework;
using Newtonsoft.Json;

namespace Accona.Monitoring.Test
{
    [TestFixture]
    public class MonitorTest
    {

        [Test]
        public void Test_Setup_Monitor()
        {
            Monitor.Setup()
                .WithPublisher<MemoryPublisher>()
                .WithExposure(new RestExposure(8080));

            var client = new WebClient();
            var stream = client.OpenRead("http://localhost:8080/Monitoring/ByFullDef/Test/Monitor.Test/SomeTag");
            var reader = new StreamReader(stream);
            var jsonData = reader.ReadToEnd();
            Console.WriteLine(jsonData);

            jsonData = JsonConvert.DeserializeObject(jsonData) as string;
            dynamic obj = JsonConvert.DeserializeObject(jsonData);
            Console.WriteLine(obj);
            Console.WriteLine(obj.AppName);
            double? maxValue = obj.MaxValue;
            Console.WriteLine(maxValue);
            double? value = obj.Value;
            Console.WriteLine(value);
        }

        [Test]
        public void Test_Monitor_Increase_By_One()
        {
            Monitor.Setup().WithPublisher<MemoryPublisher>();
            Monitor.One("TestApp", "Crosscut.Test", "ClassStats");

            var publisher = Monitor.GetPublisher<MemoryPublisher>();
            var stats = publisher.GetStatistics("TestApp", "Crosscut.Test", "ClassStats");

            Ensure.That(stats.Value).Is.EqualTo(1);
        }

        [Test]
        public void Test_Monitor_Increase_By_Three()
        {
            Monitor.Setup().WithPublisher<MemoryPublisher>();
            Monitor.Increment("TestApp", "Crosscut.Test", "ClassStats", 3);

            var publisher = Monitor.GetPublisher<MemoryPublisher>();
            var stats = publisher.GetStatistics("TestApp", "Crosscut.Test", "ClassStats");

            Ensure.That(stats.Value).Is.EqualTo(3);
        }

        [Test]
        public void Test_Monitor_Broadcast_Publish()
        {
            Monitor.Setup().WithPublisher<MemoryPublisher>();
            Monitor.Publish(new DefaultAppStatistics()
                            {
                                AppName = "TestApp", 
                                StatNamespace = "Crosscut.Test", 
                                Tag = "ClassStats",
                                Value = 100.9
                             });

            var publisher = Monitor.GetPublisher<MemoryPublisher>();
            var stats = publisher.GetStatistics("TestApp", "Crosscut.Test", "ClassStats");

            Ensure.That(stats.Value).Is.EqualTo(100.9);

            // Each publish replaces old stats in MemoryPublisher
            Monitor.Publish(new DefaultAppStatistics()
            {
                AppName = "TestApp",
                StatNamespace = "Crosscut.Test",
                Tag = "ClassStats",
                Value = 50.3
            });

            stats = publisher.GetStatistics("TestApp", "Crosscut.Test", "ClassStats");
            Ensure.That(stats.Value).Is.EqualTo(50.3);
            
        }



    }
}
